import {defineStore} from "pinia";

import {getMenuTree} from "@/api/basicsApi/systemManage/MenuManage.ts";
import {getRoleMenuRel} from "@/api/basicsApi/systemManage/RoleManage.ts";
import {MenuOperationInfo, MenuTreeRes} from "@/interface/basicsInterface/systemManage";

export const useUserInfoStore = defineStore("userInfo", {
    state: (): UserInfosState => ({
        userInfos: {
            init: false,//判断是否获取过路由
        },
    }),
    actions: {
        // 获取所有菜单树
        getPagesInfo(): Promise<MenuTreeRes[]> {
            return new Promise((resolve, reject) => {
                getMenuTree().then((res: any) => {
                    const {data} = res
                    resolve(data)
                }).catch((error: any) => {
                    this.userInfos.init = false
                    reject(error)
                })
            })
        },
        // 获取角色所拥有的菜单
        getRoleMenuRelFun(roleId: number): Promise<MenuOperationInfo[]> {
            return new Promise((resolve, reject) => {
                getRoleMenuRel(roleId).then((res: any) => {
                    const {data} = res
                    resolve(data)
                }).catch((error: any) => {
                    reject(error)
                })
            })
        }
    }
});
